function pathToRegxp(path, keys, options) {
  path = "^" + path;
  path = path.replace(/\:(\w+)/g, function (match, key) {
    keys.push(key);
    return "([^/]+)";
  });
  path += options.end === false ? "(?=/|$)" : "$";

  return new RegExp(path, "i");
}

function Layer(path, options = {}, fn) {
  this.path = "";
  this.handle = fn;
  this.keys = [];
  this.params = {};
  this.regxp = pathToRegxp(path, this.keys, options);
  this.fast_slash = path === "/" && options.end === false;
}

Layer.prototype.match = function (path) {
  if (this.fast_slash) return true;

  let match = this.regxp.exec(path);
  console.log("🚀 ～ file: layer.js:20 ～ match:", match);
  console.log(this.keys);

  if (!match) return false;
  this.path = match[0];

  for (let index = 0; index < this.keys.length; index++) {
    this.params[this.keys[index]] = match[index + 1];
  }
  console.log(this.params);

  return true;
};

Layer.prototype.handle_error = function (err, req, res, next) {
  const fn = this.handle;

  if (fn.length !== 4) {
    return next(err);
  }
  fn(err, req, res, next);
};

Layer.prototype.handle_request = function (req, res, next) {
  const fn = this.handle;

  if (fn.length !== 3) {
    return next();
  }
  fn(req, res, next);
};

module.exports = Layer;
